package com.cloudeasy.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudeasy.server.mapper.RoleMapper;
import com.cloudeasy.server.po.Permission;
import com.cloudeasy.server.po.Role;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleService extends ServiceImpl<RoleMapper, Role> {
    private final RolePermissionService rolePermissionService;

    public RoleService(RolePermissionService rolePermissionService) {
        this.rolePermissionService = rolePermissionService;
    }

    /**
     * 获取可用角色信息
     *
     * @param ids 角色Id列表
     * @return {@link List<Role>}
     */
    public List<Role> getEnableListByIds(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            return null;
        }
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids)
                .eq("enable", true);
        return this.list(queryWrapper);
    }

    /**
     * 获取可用权限
     *
     * @param ids Id列表
     * @return {@link List<Permission>}
     */
    public List<Permission> getEnablePermission(List<String> ids) {
        List<String> roleIds = getEnableListByIds(ids).stream().map(Role::getId).collect(Collectors.toList());
        if (roleIds.size() == 0) {
            return null;
        }
        return rolePermissionService.getEnablePermission(roleIds);
    }
}
